﻿using CMS.Business.ViewModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Transactions;
using CMS.DataAccess;
namespace CMS.Business
{
    public partial class PageBussiness : BaseBussiness<PageViewModel>, IPageBussiness
    {
        protected override void DoAdd(PageViewModel entity)
        {
            throw new NotImplementedException();
        }

        protected override void DoUpdate(PageViewModel entity)
        {
            throw new NotImplementedException();
        }

        protected override void DoDelete(PageViewModel entity)
        {
            throw new NotImplementedException();
        }

        protected override void DoDelete(int id)
        {
            throw new NotImplementedException();
        }

        protected override PageViewModel DoSelect(int id)
        {
            throw new NotImplementedException();
        }

        public List<PageViewModel> GetAllPagesByLanguageId(int id)
        {
            return DatabaseFunction.GetData<List<PageViewModel>>("GetAllPagesByLanguageId", DatabaseFunction.CreateSqlParameter("@LanguageId", SqlDbType.Int, id), dr => GetAllPagesByLanguage(dr));
        }

        private List<PageViewModel> GetAllPagesByLanguage(SqlDataReader dr)
        {
            List<PageViewModel> listPageViewModel = new List<PageViewModel>();

            while (dr.Read())
            {
                PageViewModel pageViewModel = new PageViewModel();

                if (dr["PageId"] != DBNull.Value)
                {
                    pageViewModel.PageId = Convert.ToInt32(dr["PageId"].ToString());
                }

                if (dr["PageName"] != DBNull.Value)
                {
                    pageViewModel.PageName = dr["PageName"].ToString();
                }

                if (dr["Action"] != DBNull.Value)
                {
                    pageViewModel.Action = dr["Action"].ToString();
                }

                if (dr["Controller"] != DBNull.Value)
                {
                    pageViewModel.Controller = dr["Controller"].ToString();
                }

                if (dr["PageFamilyId"] != DBNull.Value)
                {
                    pageViewModel.PageFamilyId = Convert.ToInt32(dr["PageFamilyId"].ToString());
                }

                if (dr["LanguageId"] != DBNull.Value)
                {
                    pageViewModel.LanguageId = Convert.ToInt32(dr["LanguageId"].ToString());
                }

                if (dr["UserId"] != DBNull.Value)
                {
                    pageViewModel.UserId = Convert.ToInt32(dr["UserId"].ToString());
                }

                if (dr["CreatedDate"] != DBNull.Value)
                {
                    pageViewModel.CreatedDate = Convert.ToDateTime(dr["CreatedDate"].ToString());
                }

                listPageViewModel.Add(pageViewModel);
            }

            return listPageViewModel;
        }

        public List<PageSubPageViewModel> GetAllPageSubPage()
        {
            return DatabaseFunction.GetData<List<PageSubPageViewModel>>("GetAllPageSubPage", dr => GetAllPageSubPage(dr));
        }

        private List<PageSubPageViewModel> GetAllPageSubPage(SqlDataReader dr)
        {
            List<PageSubPageViewModel> listPageSubPageViewModel = new List<PageSubPageViewModel>();

            while (dr.Read())
            {
                PageSubPageViewModel pageSubPageViewModel = new PageSubPageViewModel();

                if (dr["PageId"] != DBNull.Value)
                {
                    pageSubPageViewModel.PageId = Convert.ToInt32(dr["PageId"].ToString());
                }

                if (dr["SubPageId"] != DBNull.Value)
                {
                    pageSubPageViewModel.SubPageId = Convert.ToInt32(dr["SubPageId"].ToString());
                }

                listPageSubPageViewModel.Add(pageSubPageViewModel);
            }

            return listPageSubPageViewModel;
        }


    }
}
